package edu.hubu.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.hubu.model.Users;
import edu.hubu.service.UserService;
import edu.hubu.service.impl.UserServiceImpl;

@WebServlet("/UserController")
public class UserController extends HttpServlet {
	
	private UserService userService = new UserServiceImpl();

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		req.setCharacterEncoding("utf-8");
		res.setContentType("text/html;charset=utf-8");
		PrintWriter out = res.getWriter();
		
		String type=req.getParameter("type");

/*		if ("getAllUsers".equals(type)) {
			getAllUsers(req,res);
		}*/

		if ("getPageUsers".equals(type)) {
			getPageUsers(req, res);
		} else if ("delUserById".equals(type)) {
			delUserById(req, res);
		} else if ("gotoAddView".equals(type)) {
			gotoAddView(req, res);
		} else if ("addUser".equals(type)) {
			addUser(req, res);
		}

	}

/*	private void getAllUsers(HttpServletRequest req, HttpServletResponse res) {
		List<Users> userList=userService.getAllUsers();
		req.setAttribute("userList", userList);
		try {
			req.getRequestDispatcher("/Manage").forward(req, res);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	private void addUser(HttpServletRequest req, HttpServletResponse res) {

		// 先接收各个参数
		String name = req.getParameter("name");
		String nickName = req.getParameter("nickName");
		String pwd = req.getParameter("pwd");
		String gender = req.getParameter("gender");
		String birthday = req.getParameter("birthday");
		String[] hobbies = req.getParameterValues("hobby");
		String hobby = "";
		for (String hb : hobbies) {
			hobby += hb + ",";
		}
		hobby.substring(0, hobby.lastIndexOf(','));
		String tel = req.getParameter("tel");
		String email = req.getParameter("email");
		String grade = req.getParameter("grade");
		String description = req.getParameter("description");

		// 再封装user
		Users users = null;
		try {
			users = new Users(name, nickName, pwd, gender,
					new SimpleDateFormat("yyyy-MM-dd").parse(birthday), hobby,
					tel, email, Integer.parseInt(grade), description);
		} catch (Exception e) {
			e.printStackTrace();
		}

		userService.addUser(users);

	}

	private void gotoAddView(HttpServletRequest req, HttpServletResponse res) {

		try {
			req.getRequestDispatcher("/AddView").forward(req, res);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void delUserById(HttpServletRequest req, HttpServletResponse res) {

		String id = req.getParameter("id");

		try {
			userService.delUserById(Integer.parseInt(id));
			getPageUsers(req, res);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void getPageUsers(HttpServletRequest req, HttpServletResponse res) {

		int pageNum = 1;
		int pageSize = 5;
		int pageCount = 0;

		String num = req.getParameter("pageNum");
		String size = req.getParameter("pageSize");

		if (num != null && !"".equals(num)) {
			pageNum = Integer.parseInt(num);
		}

		if (size != null && !"".equals(size)) {
			pageSize = Integer.parseInt(size);
		}

		List<Users> userList = userService.getPageUsers(pageNum, pageSize);
		pageCount = userService.getPageCount(pageSize);
		req.setAttribute("pageCount", pageCount);
		req.setAttribute("pageNum", pageNum);
		req.setAttribute("userList", userList);
		try {
			req.getRequestDispatcher("/Manage").forward(req, res);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		this.doGet(req, res);

	}

}
